<template>
  <div class>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="mt15">
      <!-- 添加分类按钮 -->
      <div class="t-left">
        <el-button type="primary" @click="showAddCateDia">添加分类</el-button>
      </div>
      <!-- 树形表格 -->
      <tree-table
        :data="catesList"
        :columns="columns"
        border
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        class="mt15"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <!-- {{scope.row.cat_deleted}} -->
          <i v-if="scope.row.cat_deleted==false" class="el-icon-success cGreen"></i>
          <i v-else class="el-icon-error cRed"></i>
        </template>

        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <!-- {{scope.row.cat_level}} -->
          <el-tag v-if="scope.row.cat_level==0" type="warning">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level==1" type="info">二级</el-tag>
          <el-tag v-else>三级</el-tag>
        </template>

        <!-- 操作 -->
        <template slot="editDel" slot-scope="scope" width="100px">
          <!-- {{scope.row.cat_level}} -->
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteCate(scope.row.cat_id)"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        class="pagBox"
      ></el-pagination>
    </el-card>

    <!-- 添加分类弹窗 -->
    <el-dialog title="添加分类" :visible.sync="addCatedialogVisible" width="30%" @close="addCateClose">
      <!-- 表单区域 -->
      <el-form
        ref="addCateformRef"
        :model="addCateform"
        :rules="addCateformRules"
        label-width="80px"
        expandTrigger="hover"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateform.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- 级联菜单 -->
          <!-- 
            v-model双向绑定的数据
            options数据源
            props配置项---显示哪个字段 对应的哪个值  hover触发
          -->
          <el-cascader
            v-model="selectedParCates"
            :options="parentCates"
            :props="addCateProp"
            @change="inputSelectChange"
            class="width100"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      // 分类数据
      catesList: [],
      // 获取分类数据的参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 当前一级分类总数
      total: 0,
      // 配置表格中的选项
      columns: [
        { label: "分类名称", prop: "cat_name" },
        { label: "是否有效", prop: "", type: "template", template: "isok" },
        { label: "排序", prop: "", type: "template", template: "order" },
        { label: "操作", prop: "", type: "template", template: "editDel" },
      ],
      // 添加分类弹窗
      addCatedialogVisible: false,
      // 添加分类弹窗数据
      addCateform: {
        cat_pid: "",
        cat_name: "",
        cat_level: "",
      },
      // 添加分类框的验证规则
      addCateformRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      // 父级分类数据
      parentCates: [],
      // 级联菜单数据
      selectedParCates: [],
      // 级联菜单配置项
      addCateProp: {
        // 定义显示字段
        label: "cat_name",
        // 定义选择传到绑定数组里的字段
        value: "cat_id",
        // 定义级联项
        children: "children",
        // 定义是鼠标点击还是hover
        expandTrigger: "hover",
        checkStrictly: true,
      },
    };
  },
  created() {
    this.getCatesList();
  },
  methods: {
    // 创建组件时直接获取用户数据
    getCatesList() {
      // this.$axios.get('categories?type=3&pagesize=1&pagenum=5').then(val=>{
      this.$axios.get("categories", { params: this.queryInfo }).then((val) => {
        // console.log(val);
        if (val.meta.status != 200) {
          return this.$message.error(val.meta.msg);
        }
        this.catesList = val.data.result;
        this.total = val.data.total;
      });
    },
    // 页码改变时
    handleCurrentChange(newnum) {
      this.queryInfo.pagenum = newnum;
      this.getCatesList();
    },
    // 单页数据改变时
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getCatesList();
    },

    // 单击添加分类按钮时
    showAddCateDia() {
      // 请求父级分类的数据
      this.$axios.get("categories?type=2").then((val) => {
        console.log(val);
        if (val.meta.status != 200) {
          return this.$message.error(val.meta.msg);
        }
        this.parentCates = val.data;
      });

      // 打开对话框
      this.addCatedialogVisible = true;
    },
    // 添加分类窗口关闭时
    addCateClose() {
      this.$refs.addCateformRef.resetFields();
      this.selectedParCates = [];
    },
    // 提交添加分类窗口时
    submitAddCate() {
      // 判断表格里的值是否复合规则
      this.$refs.addCateformRef.validate((vid) => {
        if (!vid) {
          return;
        }

        if (this.selectedParCates.length > 0) {
          // 设置父级分类id
          this.addCateform.cat_pid = this.selectedParCates[
            this.selectedParCates.length - 1
          ];
          // 设置分类级别
          this.addCateform.cat_level = this.selectedParCates.length;
        } else {
          this.addCateform.cat_pid = 0;
          this.addCateform.cat_level = 0;
        }

        // 发送请求添加分类
        this.$axios.post("categories", this.addCateform).then((val) => {
          console.log(val);
          if (val.meta.status != 201) {
            return this.$message.error(val.meta.msg);
          }
          // 再加载一次列表
          this.getCatesList();
          // 关闭对话框
          this.addCatedialogVisible = false;
        });
      });
    },
    // 点选级联菜单时
    inputSelectChange() {
      console.log(this.selectedParCates);
    },
    // 删除分类
    deleteCate(id) {
      // console.log(id);
      this.$axios.delete("categories/" + id).then((val) => {
        // console.log(val);
        if (val.meta.status != 200) {
          return this.$message.error(val.meta.msg);
        }
        this.getCatesList()
        this.$message.success(val.meta.msg);
      });
    },
  },
};
</script>
<style lang="css" scoped>
.mt15 {
  margin-top: 15px;
}
.t-left {
  text-align: left;
}
.cGreen {
  color: green;
}
.cRed {
  color: red;
}
.width100 {
  width: 100%;
}
</style>